package com.ebai.product.management.framework.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ebai.product.management.model.entity.Auth;
import com.ebai.product.management.model.vojo.AuthTreeVo;

/**
 * 菜单权限构建树结构数据
 * @author mgli
 *
 */
public class TreeUtils {

	/**
     * 根据父节点的ID获取所有子节点
     * 
     * @param list 分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public static List<Auth> getChildPerms(final List<Auth> list, final String parentId) {
        List<Auth> returnList = new ArrayList<Auth>();
        for (Iterator<Auth> iterator = list.iterator(); iterator.hasNext();) {
            Auth t = (Auth) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (StringUtils.equals(t.getParentAuth(), parentId)) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     * 
     * @param list
     * @param t
     */
    private static void recursionFn(List<Auth> list, Auth t) {
        // 得到子节点列表
        List<Auth> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Auth tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<Auth> it = childList.iterator();
                while (it.hasNext()) {
                    Auth n = (Auth) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private static List<Auth> getChildList(List<Auth> list, Auth t) {
        List<Auth> tlist = new ArrayList<Auth>();
        Iterator<Auth> it = list.iterator();
        while (it.hasNext()) {
            Auth n = (Auth) it.next();
            if (StringUtils.equals(n.getParentAuth(), t.getPid())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private static boolean hasChild(List<Auth> list, Auth t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }
    
    // TODO 获取权限树列表数据****************************************************************************************************************
    /**
     * 根据父节点的ID获取所有子节点
     * 
     * @param list 分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public static List<AuthTreeVo> getTreePerms(final List<Auth> list, final String parentId) {
        List<AuthTreeVo> returnList = new ArrayList<AuthTreeVo>();
        AuthTreeVo authTreeVo = null;
        for (Iterator<Auth> iterator = list.iterator(); iterator.hasNext();) {
            Auth t = (Auth) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (StringUtils.equals(t.getParentAuth(), parentId)) {
            	iterator.remove(); //移除根节点
            	
                authTreeVo = new AuthTreeVo();
                authTreeVo.setId(t.getPid());
                authTreeVo.setLabel(t.getDisplayName());
                recursionTree(list, authTreeVo);
                returnList.add(authTreeVo);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     * 
     * @param list
     * @param t
     */
    private static void recursionTree(List<Auth> list, AuthTreeVo authTreeVo) {
        // 得到子节点列表
        List<AuthTreeVo> childList = getChildTreeList(list, authTreeVo.getId(), authTreeVo);
        authTreeVo.setChildren(childList);
        for (AuthTreeVo tChild : childList) {
        	// 判断是否有子节点
            if (hasChildTree(list, tChild.getId(), authTreeVo)) {
                recursionTree(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private static List<AuthTreeVo> getChildTreeList(List<Auth> list, String parentAuthid, AuthTreeVo authTreeVo) {
        List<AuthTreeVo> tlist = new ArrayList<AuthTreeVo>();
        Iterator<Auth> it = list.iterator();
        AuthTreeVo childAuthTreeVo = null;
        while (it.hasNext()) {
            Auth n = (Auth) it.next();
            childAuthTreeVo = new AuthTreeVo();
            childAuthTreeVo.setId(n.getPid());
            childAuthTreeVo.setLabel(n.getDisplayName()+" "+(null==n.getAuthPerms()?"":n.getAuthPerms()));
            if (StringUtils.equals(n.getParentAuth(), parentAuthid)) {
                tlist.add(childAuthTreeVo);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private static boolean hasChildTree(List<Auth> list, String parentAuthid, AuthTreeVo authTreeVo) {
        return getChildTreeList(list, parentAuthid, authTreeVo).size() > 0 ? true : false;
    }
}

